from model.engine import GameObject, GameEngine, JUMP_VEL
from event.event_types import EVENT_UPDATE
import unittest
import numpy

class EventManagerMock:
    received = []
    def post(self, event):
        self.received.append(event)

class EngineTestCase(unittest.TestCase):

    POS_1 = (0,0,0)
    POS_2 = (1,2,3)
    VEL_2 = (0,0,0)
    TAR_2 = (1,2,3)
    POS_3 = (1.,2.,3.)
    VEL_3 = (0.,1.,0.)
    TAR_3 = (2.,2.,0.)

    def setUp(self):
        self.engine = GameEngine(
            ((self.POS_1,),
             (self.POS_2, self.VEL_2, self.TAR_2),
             (self.POS_3, self.VEL_3, self.TAR_3)))
        self.ev_manager = EventManagerMock()

    def test_init(self):
        self.assertTrue((self.engine.objects[0].position == self.POS_1).all())
        self.assertTrue((self.engine.objects[1].position == self.POS_2).all())
        self.assertTrue((self.engine.objects[2].position == self.POS_3).all())
        self.assertTrue((self.engine.objects[0].velocity == (0.,0.,0.)).all())
        self.assertTrue((self.engine.objects[1].velocity == self.VEL_2).all())
        self.assertTrue((self.engine.objects[2].velocity == self.VEL_3).all())

    def test_jump(self):
        self.engine.jump(0)
        self.assertTrue((self.engine.objects[0].velocity == JUMP_VEL).all())
        self.assertTrue((self.engine.objects[1].velocity == self.VEL_2).all())
        self.assertTrue((self.engine.objects[2].velocity == self.VEL_3).all())
        self.engine.jump(1)
        self.engine.jump(2)
        self.assertTrue((self.engine.objects[0].velocity == JUMP_VEL).all())
        self.assertTrue((self.engine.objects[1].velocity == JUMP_VEL + self.VEL_2).all())
        self.assertTrue((self.engine.objects[2].velocity == JUMP_VEL + self.VEL_3).all())

    def test_update(self):
        self.engine.update_all(self.ev_manager, 1000)
        NEW_POS_1 = self.POS_1
        NEW_POS_2 = (1,2,0)
        NEW_POS_3 = (21.,1002.,0.)
        NEW_VEL_1 = (0.,0.,0.)
        NEW_VEL_2 = (0,0,-9.81)
        NEW_VEL_3 = (0.02,1.,-9.81)
        self.assertTrue((self.engine.objects[0].position == NEW_POS_1).all())
        self.assertTrue((self.engine.objects[1].position == NEW_POS_2).all())
        self.assertTrue((self.engine.objects[2].position == NEW_POS_3).all())
        self.assertTrue((self.engine.objects[0].velocity == NEW_VEL_1).all())
        self.assertTrue((self.engine.objects[1].velocity == NEW_VEL_2).all())
        self.assertTrue((self.engine.objects[2].velocity == NEW_VEL_3).all())
        self.assertTrue(len(self.ev_manager.received) == 3)
        self.assertTrue(self.ev_manager.received[0].type == EVENT_UPDATE)
        
if __name__ == '__main__':
    unittest.main()

